//
//  RWBillViewController.m
//  RedWine
//
//  Created by Liufangfang on 2017/1/14.
//  Copyright © 2017年 HuiBangKe. All rights reserved.
//

#import "RWBillViewController.h"
#import "RWNormalBillView.h"
#import "RWIncrementBillView.h"
#import "FTYInfoVertifyTools.h"

#define kBillButtonTagBase 500
#define kHeaderHeight 100.
#define kFooterHeight 50.
#define kContentHeight 400

@interface RWBillViewController ()<UITextFieldDelegate>{
    
    NSArray *normalBillContents;
    NSArray *incrementBillTitles;
}

@property (nonatomic, strong) RWNormalBillView *normalBillV;
@property (nonatomic, strong) RWIncrementBillView *incrementBillV;

@property (nonatomic, copy) NSString *billParam;

@end

@implementation RWBillViewController

#pragma mark - View LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initObjects];
    [self setNavigation];
    [self setBillUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Load Datas

- (void)initObjects{
    
    _billStyle = RWNoBillViewStyle;
    normalBillContents = @[@"酒", @"礼品", @"食品"];
    incrementBillTitles = @[@"发票抬头：", @"寄货地址：", @"公司电话：", @"开户行：", @"银行账号：", @"税务号："];
}

- (void)loadDatas{
    
    [FTYHTTPManager postInfoWithSubUrl:[NSString stringWithFormat:@"/shop/ybkapi.php?act=ybkapi&op=addresslist&member_id=%@", [FTYUser sharedUser].userID] parameters:nil block:^(id result, NSError *error) {
        if (!error) {
            
            DLog(@"====%@", result);
            FTYBaseModel *baseModel = [[FTYBaseModel alloc] init];
            baseModel = [FTYBaseModel mj_objectWithKeyValues:result];
            FTYErrorInfoModel *errorInfo = [FTYErrorInfoModel mj_objectWithKeyValues:baseModel.err];
            
            if (0 == errorInfo.errorcode) {
                
            } else {
                [[FTYToastView alloc] addTitle:errorInfo.errorinfo addView:self.view];
            }
        } else {
            DLog(@"地址获取信息失败：%@", result);
        }
    }];
}

#pragma mark - Config UI

- (void)setNavigation{
    
    self.title = @"发票信息";
}

- (void)setBillUI{

    self.view.backgroundColor = [FTYColor getEEEEEEColorWithAlpha:1.];
    [self setHeaderUI];
    [self setBillContentUIWithFlag:_billStyle];
    [self setFooterUI];
}

#pragma mark - Config Invoke Methods

- (void)setHeaderUI{

    NSArray *bills = @[@"不开发票", @"普通发票", @"增值税发票"];
    
    UIView *headerV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FTY_SCREEN_WIDTH, kHeaderHeight*FTY_HEIGHT_BASE)];
    [self.view addSubview:headerV];
    headerV.backgroundColor = [UIColor whiteColor];
    
    UILabel *billInfoLab = [[UILabel alloc] init];
    [headerV addSubview:billInfoLab];
    billInfoLab.text = @"发票信息";
    billInfoLab.textColor = [UIColor blackColor];
    billInfoLab.font = FTY_FONTTSIZE(14);
    [billInfoLab sizeToFit];
    [billInfoLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_offset(10.*FTY_HEIGHT_BASE);
        make.height.mas_offset(20.*FTY_HEIGHT_BASE);
    }];
    
    for (int i=0; i<3; i++) {
        
        float btnW = 100.;
        
        UIButton *billBtn = [[UIButton alloc] init];
        [headerV addSubview:billBtn];
        billBtn.backgroundColor = [FTYColor getEEEEEEColorWithAlpha:1.];
        [billBtn setTitle:bills[i] forState:UIControlStateNormal];
        [billBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        billBtn.titleLabel.font = FTY_FONTTSIZE(15);
        billBtn.tag = kBillButtonTagBase + i;
        [billBtn addTarget:self action:@selector(billBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [billBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(billInfoLab.mas_bottom).offset(10.*FTY_HEIGHT_BASE);
            make.left.mas_equalTo(15 * FTY_WIDTH_BASE + (btnW + 25) * i * FTY_WIDTH_BASE);
            make.width.mas_equalTo(btnW * FTY_WIDTH_BASE);
            make.height.mas_equalTo(35.*FTY_HEIGHT_BASE);
        }];
    }
}

- (void)setBillContentUIWithFlag:(RWBillViewStyle)billStyle{
    
    switch (billStyle) {
            
        case RWNormalBillViewStyle:
            
            _normalBillV = [[RWNormalBillView alloc] initWithFrame:CGRectMake(0, kHeaderHeight, FTY_SCREEN_WIDTH, 200)];
            [self.view addSubview:_normalBillV];
            self.view.backgroundColor = [UIColor redColor];
            _normalBillV.backgroundColor = [FTYColor getEEEEEEColorWithAlpha:1.];
            
            break;
        case RWIncrementBillViewStyle:
            
            _incrementBillV = [[RWIncrementBillView alloc] initWithFrame:CGRectMake(0, kHeaderHeight, FTY_SCREEN_WIDTH, 400)];
            [self.view addSubview:_incrementBillV];
            _incrementBillV.backgroundColor = [FTYColor getEEEEEEColorWithAlpha:1.];
            break;
            
        default:
            break;
    }
}

- (void)setFooterUI{
    
    UIView *footerV = [[UIView alloc] initWithFrame:CGRectMake(0, kContentHeight*FTY_HEIGHT_BASE, FTY_SCREEN_WIDTH, 500*FTY_HEIGHT_BASE)];
    [self.view addSubview:footerV];
    footerV.backgroundColor = [UIColor whiteColor];
    
    FTYButton *confirmBtn = [FTYButton createButtonWithTitle:@"确定" tColor:[UIColor blackColor] fontSize:16.*FTY_WIDTH_BASE backGroundColor:[FTYColor getMainColorWithAlpha:1.] cornerRadius:15.*FTY_WIDTH_BASE];
    [footerV addSubview:confirmBtn];
    [confirmBtn addTarget:self action:@selector(confirmBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(130.*FTY_HEIGHT_BASE);
        make.left.mas_equalTo(10.*FTY_WIDTH_BASE);
        make.width.mas_equalTo(FTY_SCREEN_WIDTH - 15.*FTY_WIDTH_BASE *2);
        make.height.mas_equalTo(40.*FTY_HEIGHT_BASE);
    }];
}

#pragma mark - Private Invoke Methods

- (void)billBtnAction:(UIButton *)btn{

    if (_normalBillV) {
        [_normalBillV removeFromSuperview];
    }
    if(_incrementBillV){
        [_incrementBillV removeFromSuperview];
    }
    switch (btn.tag) {
        case 500:
            _billStyle = RWNoBillViewStyle;
            break;
        case 501:{
            
            _billStyle = RWNormalBillViewStyle;
            _normalBillV = [[RWNormalBillView alloc] initWithFrame:CGRectMake(0, kHeaderHeight, FTY_SCREEN_WIDTH, kContentHeight*FTY_HEIGHT_BASE)];
            [self.view addSubview:_normalBillV];
            break;
        }
        case 502:{
            
            _billStyle = RWIncrementBillViewStyle;
            _incrementBillV = [[RWIncrementBillView alloc] initWithFrame:CGRectMake(0, kHeaderHeight, FTY_SCREEN_WIDTH, kContentHeight*FTY_HEIGHT_BASE)];
            [self.view addSubview:_incrementBillV];
            break;
        }
        default:
            break;
    }
}

- (void)confirmBtnAction:(UIButton *)btn{
    
    switch (_billStyle) {
            //不开发票
        case RWNoBillViewStyle:
            [self noBill];
            break;
            //普通发票
        case RWNormalBillViewStyle:{
            [self normalBill];
            break;
        }
            //增值税发票
        case RWIncrementBillViewStyle:{
            [self incrementBill];
            break;
        }
            
        default:
            break;
    }
}

//普通发票
- (void)normalBill{

    NSString *normalBillTaiTou = _normalBillV.textField.text;
    DLog(@"_normalBillV.selectedBtn.tag:%ld",_normalBillV.selectedBtn.tag);
    NSString *content = @"酒";
    switch (_normalBillV.selectedBtn.tag) {
        case 0:
            content = @"酒";
            break;
        case 1:
            content = @"礼品";
            break;
        case 2:
            content = @"食品";
            break;
            
        default:
            break;
    }
    
    
    if ([FTYInfoVertifyTools isnilVertify:normalBillTaiTou]) {
        [[FTYToastView alloc] addTitle:@"发票抬头不能为空" addView:self.view];
    } else {
        
        //拼接普通发票字符串参数
        _billParam = [NSString stringWithFormat:@"inv=1&inv_title=%@&inv_content=%@", normalBillTaiTou, content];
        //将参数传给订单页
        [self sendJointBillParam];
    }
}

//不开发票
- (void)noBill{

    _billParam = @"";
    [self sendJointBillParam];
}

//增值税发票
- (void)incrementBill{

    NSString *incrementBillTaiTou = _incrementBillV.billTaiTouTF.text;
    NSString *addr = _incrementBillV.addrTF.text;
    NSString *phoneNum = _incrementBillV.phoneNumTF.text;
    NSString *bank = _incrementBillV.bankTF.text;
    NSString *bankCard = _incrementBillV.bankCardTF.text;
    NSString *taxID = _incrementBillV.taxIDTF.text;
    
    if ([FTYInfoVertifyTools isnilVertify:incrementBillTaiTou]) {
        [[FTYToastView alloc] addTitle:@"发票抬头不能为空" addView:self.view];
    } else if([FTYInfoVertifyTools isnilVertify:addr]){
        [[FTYToastView alloc] addTitle:@"请输入正确的寄货地址" addView:self.view];
    }  else if([FTYInfoVertifyTools isnilVertify:phoneNum]){
        [[FTYToastView alloc] addTitle:@"请输入正确公司电话" addView:self.view];
    } else if([FTYInfoVertifyTools isnilVertify:bank]){
        [[FTYToastView alloc] addTitle:@"请输入正确的开户行" addView:self.view];
    } else if([FTYInfoVertifyTools isnilVertify:bankCard]){
        [[FTYToastView alloc] addTitle:@"请输入正确的银行账号" addView:self.view];
    } else if([FTYInfoVertifyTools isnilVertify:taxID]){
        [[FTYToastView alloc] addTitle:@"请输入正确的税务号" addView:self.view];
    } else {
        
        //拼接增值税发票字符串参数
        _billParam = [NSString stringWithFormat:@"inv=2&inv_title=%@&address=%@&phone=%@&bank=%@&bank_account=%@&inv_code=%@", incrementBillTaiTou, addr, phoneNum, bank, bankCard, taxID];
        //将参数传给订单页
        [self sendJointBillParam];
    }
}

- (void)sendJointBillParam{
    
    //思：通知传参过不去
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"SendJointBillParamToOrderNotification" object:@{@"jointBillParam": _billParam}];
    
    if (_delegate && [_delegate respondsToSelector:@selector(sendDataWithParam:)]) {
        [_delegate sendDataWithParam:_billParam];
    }
    [self.navigationController popViewControllerAnimated:YES];
}



@end
